//
//  YunOrderListCell.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/22.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "YunOrderListCell.h"
#import "YunOrderListModel.h"

@interface YunOrderListCell ()
@property(nonatomic, strong) UIView *topLineView;
@property(nonatomic, strong) UILabel *orderLabel;
@property(nonatomic, strong) UIImageView *sellerImageView;
@property(nonatomic, strong) UILabel *sellerLabel;
@property(nonatomic, strong) UILabel *timeLabel;
@property(nonatomic, strong) UILabel *amountLabel;
@property(nonatomic, strong) UILabel *statusLabel;
@property(nonatomic, strong) UIButton *payButton;
@end

@implementation YunOrderListCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColor.whiteColor;
        self.backgroundColor = UIColor.whiteColor;
        [self.contentView addSubview:self.topLineView];
        [self.contentView addSubview:self.orderLabel];
        [self.contentView addSubview:self.sellerImageView];
        [self.contentView addSubview:self.sellerLabel];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.amountLabel];
        [self.contentView addSubview:self.statusLabel];
        [self.contentView addSubview:self.payButton];
        [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.height.mas_equalTo(5);
        }];
        [self.orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(self.topLineView.mas_bottom).mas_equalTo(15);
            make.right.mas_equalTo(-15);
        }];
        [self.sellerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(self.orderLabel.mas_bottom).mas_equalTo(20);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        [self.sellerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.sellerImageView.mas_right).mas_equalTo(5);
            make.centerY.mas_equalTo(self.sellerImageView);
            make.right.mas_equalTo(-15);
        }];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(self.sellerImageView.mas_bottom).mas_equalTo(20);
            make.right.mas_equalTo(-15);
        }];
        [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.sellerLabel);
            make.right.mas_equalTo(-15);
        }];
        [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.orderLabel);
            make.right.mas_equalTo(-15);
        }];
        [self.payButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.timeLabel);
            make.size.mas_equalTo(CGSizeMake(80, 28));
        }];
    }
    return self;
}
- (void)handleEvent {
    [super handleEvent];
    [self.payButton addTarget:self action:@selector(pay:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setData:(YunOrderListModel *)data {
    _data = data;
    self.orderLabel.text = [NSString stringWithFormat:@"订单号：%@",data.orderId];
    self.sellerLabel.text = data.sellerName;
    self.timeLabel.text = data.createTime;
    self.statusLabel.text = data.statusDes;
    self.amountLabel.text = [NSString stringWithFormat:@"总金额：%@",[Utilities changeDoubleToString:data.payAmount]];
    NSMutableAttributedString *amountStr1 = [[NSMutableAttributedString alloc]initWithString:@"总金额："];
    [amountStr1 setColor:rgba(153, 153, 153, 1)];
    [amountStr1 setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular]];
    
    NSMutableAttributedString *amountStr2 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  ",[Utilities removeNumberZeroWithDouble:data.payAmount]]];
    [amountStr2 setColor:rgba(51, 51, 51, 1)];
    [amountStr2 setFont:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium]];
    [amountStr1 appendAttributedString:amountStr2];
    
    if (data.amount > data.payAmount) {
        NSMutableAttributedString *amountStr3 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",[Utilities removeNumberZeroWithDouble:data.amount]]];
        [amountStr3 setColor:rgba(153, 153, 153, 1)];
        [amountStr3 setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular]];
        [amountStr3 setStrikethroughColor:rgba(153, 153, 153, 1)];
        [amountStr3 setStrikethroughStyle:NSUnderlineStyleSingle];
        
        [amountStr1 appendAttributedString:amountStr3];
    }
    self.amountLabel.attributedText = amountStr1;
    self.payButton.hidden = !data.isWaitPay;
    //self.payButton.hidden = NO;
}
- (void)pay:sender {
    if ([self.delegate respondsToSelector:@selector(cell:indexPath:extra:)]) {
        [self.delegate cell:self indexPath:self.indexPath extra:@{}];
    }
}
#pragma mark - lazy
- (UIView *)topLineView {
    if (!_topLineView) {
        _topLineView = [[UIView alloc]init];
        _topLineView.backgroundColor = UIColorHex(0xf1f1f1);
    }
    return _topLineView;
}
- (UILabel *)orderLabel {
    if (!_orderLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        lab.textColor = rgba(153, 153, 153, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _orderLabel = lab;
    }
    return _orderLabel;
}
- (UILabel *)statusLabel {
    if (!_statusLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        lab.textColor = rgba(255, 66, 66, 1);
        lab.textAlignment = NSTextAlignmentRight;
        lab.numberOfLines = 1;
        _statusLabel = lab;
    }
    return _statusLabel;
}
- (UIImageView *)sellerImageView {
    if (!_sellerImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.image = [UIImage imageNamed:@"field_dianpu"];
        _sellerImageView = iv;
    }
    return _sellerImageView;
}
- (UILabel *)sellerLabel {
    if (!_sellerLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
        lab.textColor = rgba(51, 51, 51, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _sellerLabel = lab;
    }
    return _sellerLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        lab.textColor = rgba(153, 153, 153, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _timeLabel = lab;
    }
    return _timeLabel;
}
- (UILabel *)amountLabel {
    if (!_amountLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
        lab.textColor = rgba(51, 51, 51, 1);
        lab.textAlignment = NSTextAlignmentRight;
        lab.numberOfLines = 1;
        _amountLabel = lab;
    }
    return _amountLabel;
}
- (UIButton *)payButton {
    if (!_payButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"去支付" forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        button.frame = CGRectMake(0, 0, 100, 44);
        button.layer.cornerRadius = 14;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = rgba(254, 118, 15, 1).CGColor;
        button.layer.borderWidth = 0;
        button.backgroundColor = rgba(254, 118, 15, 1);
        _payButton = button;
    }
    return _payButton;
}
@end
